package mi.payment.ui.base;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;

import java.util.List;

import mi.payment.MyApplication;
import mi.payment.R;
import mi.payment.constants.BaseErrorCode;
import mi.payment.db.PayDetailModel;
import mi.payment.db.dao.PayDetail;
import mi.payment.settings.TransactionOtherOperation;
import mi.payment.ui.other.HandWriteActivity;
import mi.payment.transaction.ResultActivity;
import mi.payment.transaction.WaitPrintActivity;

public class PageRoute {

    public static final int TYPE_CONSUME = 1;                   // 消费
    public static final int TYPE_CONSUME_REVOKE = 2;            // 消费撤销
    public static final int TYPE_RETURN_GOOD = 3;               // 退货
    public static final int TYPE_PRE_AUTH = 4;                  // 预授权
    public static final int TYPE_PRE_AUTH_REVOKE = 5;           // 预授权撤销
    public static final int TYPE_PRE_AUTH_COMPLETE = 6;         // 预授权完成
    public static final int TYPE_PRE_AUTH_COMPLETE_REVOKE = 7;  // 预授权完成撤销

    public static final int TYPE_QR_CODE_CONSUME = 10;          // 二维码消费
    public static final int TYPE_QR_CODE_REVOKE = 11;           // 二维码撤销
    public static final int TYPE_QR_CODE_RETURN_GOOD = 12;      // 二维码退货

    public static final int TYPE_ORDER_QUERY = 20;              // 订单查询

    public static void startAction(Activity activity, int type, PayDetail payDetail) {
        String answerCode = payDetail.getTradeAnswerCode();
        boolean isFreeSign = payDetail.isFreeSign;
        int transPlatform = payDetail.transPlatform;
        int qrCodeTransState = payDetail.QRCodeTransState;
        if (TextUtils.equals(answerCode, "00") && transPlatform == 0) { // 银行卡交易
            if (isFreeSign) { // 免密
                success(activity, type, payDetail);
            } else {
                // 是否电子签名
                boolean isSupportESignature = TransactionOtherOperation.getInstance().getTransactionOtherSettings().isSupportESignature();
                if (isSupportESignature) { // 支持电子签名
                    gotoESignature(activity, type, payDetail);
                } else {
                    success(activity, type, payDetail);
                }
            }
        } else if (TextUtils.equals(answerCode, "00") && transPlatform != 0 && qrCodeTransState == 1) { // 二维码交易
            success(activity, type, payDetail);
        } else {
            fail(activity, type, payDetail);
        }
        activity.finish();
    }

    // 超时操作
    public static void startTimeoutAction(Activity activity, int type, PayDetail payDetail) {
        String errorMessage = BaseErrorCode.getCode("68");
        fail(activity, type, errorMessage, payDetail);
        activity.finish();
    }

    // 无网络操作
    public static void startNoNetworkAction(Activity activity, int type, PayDetail payDetail) {
        String errorMessage = BaseErrorCode.getCode("NE");
        fail(activity, type, errorMessage, payDetail);
        activity.finish();
    }

    public static void startFailAction(Activity activity, int type, PayDetail payDetail) {
        fail(activity, type, payDetail.tradeAnswerCode, payDetail);
        activity.finish();
    }

    // 去电子签名
    private static void gotoESignature(Context context, int type, PayDetail payDetail) {
        switch (type) {
            case TYPE_CONSUME_REVOKE: // 消费撤销
                PayDetailModel.getInstance().updateIsCanceledByVoucherNo(payDetail.originalPOSNum, true);
                break;
            case TYPE_RETURN_GOOD: // 退货
                String originalReferNo = payDetail.getOriginalReferNo();
                PayDetail oriPayDetail = PayDetailModel.getInstance().getPayDetailByTransRefNo(originalReferNo);
                if (oriPayDetail != null) {
                    oriPayDetail.isCanceled = true;
                    oriPayDetail.isReturnGood = true;
                    // 退货的交易添加已退货标志
                    PayDetailModel.getInstance().update(oriPayDetail);
                }
                break;
            case TYPE_PRE_AUTH_COMPLETE_REVOKE: // 预授权完成撤销  根据终端交易流水号更新是否已撤销标志
                PayDetailModel.getInstance().updateIsCanceledByVoucherNo(payDetail.originalPOSNum, true);
                break;
        }
        HandWriteActivity.startAction(context, type, payDetail);
    }

    private static void fail(Context context, int type, PayDetail payDetail) {
        String errorMessage = BaseErrorCode.getCode(payDetail.tradeAnswerCode);
        fail(context, type, errorMessage, payDetail);
    }

    public static void success(Context context, int type, PayDetail payDetail) {
        int title;
        switch (type) {
            case TYPE_CONSUME: // 消费
            case TYPE_QR_CODE_CONSUME: // 二维码消费
                title = R.string.consume_im_title;
                break;
            case TYPE_CONSUME_REVOKE: // 消费撤销
            case TYPE_QR_CODE_REVOKE: // 二维码撤销
                title = R.string.cancellation_title;
                // 根据终端交易流水号更新是否已撤销标志
                PayDetailModel.getInstance().updateIsCanceledByVoucherNo(payDetail.originalPOSNum, true);
                break;
            case TYPE_RETURN_GOOD: // 退货
                title = R.string.return_title;
                String originalReferNo = payDetail.getOriginalReferNo();
                PayDetail oriPayDetail = PayDetailModel.getInstance().getPayDetailByTransRefNo(originalReferNo);
                if (oriPayDetail != null) {
                    oriPayDetail.isCanceled = true;
                    oriPayDetail.isReturnGood = true;
                    // 退货的交易添加已退货标志
                    PayDetailModel.getInstance().update(oriPayDetail);
                }
                break;
            case TYPE_QR_CODE_RETURN_GOOD: // 二维码退货
                title = R.string.return_title;
                List<PayDetail> list = PayDetailModel.getInstance().getPayDetailByThirdTransNumList(payDetail.thirdTransNum);
                if (list != null && list.size() > 0) {
                    for (PayDetail oriPay : list) {
                        if (oriPay.getTransactionType() == 1) {
                            oriPay.isCanceled = true;
                            oriPay.isReturnGood = true;
                            PayDetailModel.getInstance().update(oriPay);
                        }
                    }
                }
                break;
            case TYPE_PRE_AUTH: // 预授权
                title = R.string.traditional_transaction_pre_auth;
                break;
            case TYPE_PRE_AUTH_REVOKE: // 预授权撤销
                title = R.string.traditional_transaction_pre_auth_cancel;
                break;
            case TYPE_PRE_AUTH_COMPLETE: // 预授权完成
                title = R.string.pre_auth_complete;
                break;
            case TYPE_PRE_AUTH_COMPLETE_REVOKE: // 预授权完成撤销
                title = R.string.pre_authorization_complete_revoke;
                PayDetailModel.getInstance().updateIsCanceledByVoucherNo(payDetail.originalPOSNum, true);
                break;
            default:
                title = R.string.consume_im_title;
                break;
        }
        WaitPrintActivity.startAction(context, title, payDetail);
    }

    private static void fail(Context context, int type, String errorMessage, PayDetail payDetail) {
        int title;
        int hint;
        switch (type) {
            case TYPE_CONSUME: // 消费
            case TYPE_QR_CODE_CONSUME: // 二维码消费
                title = R.string.consume_im_title;
                hint = R.string.consume_result_deal;
                break;
            case TYPE_CONSUME_REVOKE: // 消费撤销
            case TYPE_QR_CODE_REVOKE: // 二维码撤销
                title = R.string.cancellation_title;
                hint = R.string.cancellation_failed;
                break;
            case TYPE_RETURN_GOOD: // 退货
            case TYPE_QR_CODE_RETURN_GOOD: // 二维码退货
                title = R.string.return_title;
                hint = R.string.return_failed;
                break;
            case TYPE_PRE_AUTH: // 预授权
                title = R.string.traditional_transaction_pre_auth;
                hint = R.string.traditional_transaction_pre_auth_fail;
                break;
            case TYPE_PRE_AUTH_REVOKE: // 预授权撤销
                title = R.string.traditional_transaction_pre_auth_cancel;
                hint = R.string.traditional_transaction_pre_auth_cancel_fail;
                break;
            case TYPE_PRE_AUTH_COMPLETE: // 预授权完成
                title = R.string.pre_auth_complete;
                hint = R.string.pre_auth_complete_fail;
                break;
            case TYPE_PRE_AUTH_COMPLETE_REVOKE: // 预授权完成撤销
                title = R.string.pre_authorization_complete_revoke;
                hint = R.string.pre_authorization_complete_revoke_fail;
                break;
            default:
                title = R.string.consume_im_title;
                hint = R.string.consume_result_deal;
                break;
        }
        ResultActivity.startAction(context, title, payDetail);
    }

    private static String getString(int resId) {
        return MyApplication.getInstance().getString(resId);
    }


}
